package com.yonyou.iuap.kettle;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.ibatis.io.Resources;

public class PropUtils {
	private PropUtils() throws Exception {
		throw new Exception("工具类不允许创建实例");
	}

	private static Properties properties;

	/**
	 * 根据给定路径获取配置文件
	 * 
	 * @param path
	 *            给定路径
	 * @return
	 */
	public static Properties getProp(String path) {
		try {
			properties = Resources.getResourceAsProperties(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	/**
	 * 根据KEY 获取conf/config.properties" 值
	 * 
	 * @return
	 */
	public static String getConfigValByKey(String key) {
		return getPropConfig().getProperty(key);
	}

	public static Properties getPropConfig() {
		return getProp("kettle/kettle.properties");
	}

	/**
	 * 修改配置文件
	 * 
	 * @param propPath
	 * @param key
	 * @param value
	 */
	public static void modifyProp(String propPath, String key, String value) {
		Properties prop = new Properties();
		try {
			prop = Resources.getResourceAsProperties(propPath);
			OutputStream out = new FileOutputStream(Resources.getResourceAsFile(propPath));
			prop.setProperty(key, value);
			prop.store(out, "modify");
			IOUtils.closeQuietly(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
